package bit.lotterysystem.service.activityStatus.operater;

import bit.lotterysystem.dao.dataobject.ActivityUserDO;
import bit.lotterysystem.dao.mapper.ActivityUserMapper;
import bit.lotterysystem.service.dto.ConvertActivityStatusDTO;
import bit.lotterysystem.service.enums.ActivityUserStatusEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Component
public class UserOperater extends AbstractActivityOperater{
    @Autowired
    private ActivityUserMapper activityUserMapper;

    @Override
    public Boolean needConvert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        Long activityId = convertActivityStatusDTO.getActivityId();
        List<Long> userIds=convertActivityStatusDTO.getUserIds();
        ActivityUserStatusEnum targetUserStatus=convertActivityStatusDTO.getTargetUserStatus();
        if(activityId==null || userIds==null || targetUserStatus==null)
            return false;
        List<ActivityUserDO>activityUserDOList=
                activityUserMapper.batchSelectByAUIds(activityId,userIds);

        if(CollectionUtils.isEmpty(activityUserDOList)){
            return false;
        }
        for(ActivityUserDO auDO:activityUserDOList){
            if(auDO.getStatus()
                    .equalsIgnoreCase(targetUserStatus.name()))
                return false;
        }
        return true;
    }

    @Override
    public boolean convert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        Long activityId = convertActivityStatusDTO.getActivityId();
        List<Long> userIds=convertActivityStatusDTO.getUserIds();
        ActivityUserStatusEnum targetUserStatus=convertActivityStatusDTO.getTargetUserStatus();
        try{
            activityUserMapper.batchUpdateStatus(activityId,userIds,targetUserStatus.name());
            return true;
        }catch (Exception e){
            return false;
        }


    }

    @Override
    public Integer sequence() {
        return 1;
    }
}
